"""
Case Type   : 功能测试
Case Name   : 主节点流控参数设置大于半分钟，备1开启实时构建流控参数设置为3
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.主节点设置流控参数为30
gs_guc set -N NODE -c 'recovery_time_target=30';
    2.备1开启实时构建
gs_guc set -N NODE1 -c 'recovery_parse_workers=4';
gs_guc set -N NODE1 -c 'recovery_redo_workers=4';
gs_guc set -N NODE1 -c 'host_standby=off';
gs_guc set -N NODE1 -c 'ss_enable_ondemand_recovery=on';
gs_guc set -N NODE1 -c 'ss_enable_ondemand_realtime_build=on';
    备1设置流控参数为3
gs_guc set -N NODE -c 'recovery_time_target=3';
    重启集群
cm_ctl stop; cm_ctl start
    3.查看日志
    4.主节点设置流控参数为60
gs_guc set -N NODE -c 'recovery_time_target=60';
    重启集群
cm_ctl stop; cm_ctl start
    5.查看日志
    6.恢复环境
Expect      :
    1.设置成功
    2.成功
    3.流控开启
    4.成功
    5.流控开启
    6.成功
History     :
"""

import unittest
import os
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from testcase.SS_FUNC.realtime_build_control.Realtime_Build_Control_Utils import rbcu
from yat.test import Node
logger = Logger()

class TestRealtimeBuild_Control0013(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.com = Common()
        self.rbcu = rbcu()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.sta_node = Node('Standby1DbUser')
        self.recovery_time_target = {}
        self.real_time = {}

        logger.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)
        self.pri_hostname = self.pri_node.sh('hostname').result()
        logger.info(f'主节点hostname: {self.pri_hostname}')
        self.sta_hostname = self.sta_node.sh('hostname').result()
        logger.info(f'备1节点hostname: {self.sta_hostname}')

    def test_shared_storage(self):

        text = '-----step1:主节点设置流控参数为30 expect:设置成功-----'
        logger.info(text)
        self.recovery_time_target = self.rbcu.set_control(self.pri_hostname, 30, True)

        text = '-----step2:备1开启实时构建、设置流控参数为3 expect:设置成功-----'
        logger.info(text)
        self.real_time = self.rbcu.set_real_time(self.sta_hostname, True)
        self.rbcu.set_real_time(self.sta_hostname)
        self.rbcu.set_control(self.sta_hostname, 3)
        logger.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        logger.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败')
        
        text = '-----step3:查看日志信息 expect:查到相关信息-----'
        logger.info(text)
        ret = self.rbcu.control_check(self.pri_node, '6001')
        self.assertIn('enable realtime-build log ctrl', ret, '主节点校验失败')
        self.assertIn('inst_id: 1', ret, '主节点校验失败')
        ret = self.rbcu.send_control_check(self.sta_node, '6002')
        self.assertIn('send primary node 0', ret, '备节点校验失败')
        ret = self.rbcu.control_check(self.sta_node, '6002')
        self.assertTrue(ret == '', '备节点校验失败')
        ret = self.rbcu.query_view(self.pri_sh)
        self.assertIn('(1 row)', ret, '主节点校验失败')
        ret = self.rbcu.query_view(self.sta_sh)
        self.assertIn('(0 rows)', ret, '备节点校验失败')

        text = '-----step4:主节点设置流控参数为60 expect:设置成功-----'
        logger.info(text)
        self.rbcu.set_control(self.pri_hostname, 60)
        logger.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        logger.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败')
        
        text = '-----step5:查看日志信息 expect:查到相关信息-----'
        logger.info(text)
        ret = self.rbcu.control_check(self.pri_node, '6001')
        self.assertIn('enable realtime-build log ctrl', ret, '主节点校验失败')
        self.assertIn('inst_id: 1', ret, '主节点校验失败')
        ret = self.rbcu.send_control_check(self.sta_node, '6002')
        self.assertIn('send primary node 0', ret, '备节点校验失败')
        ret = self.rbcu.control_check(self.sta_node, '6002')
        self.assertTrue(ret == '', '备节点校验失败')
        ret = self.rbcu.query_view(self.pri_sh)
        self.assertIn('(1 row)', ret, '主节点校验失败')
        ret = self.rbcu.query_view(self.sta_sh)
        self.assertIn('(0 rows)', ret, '备节点校验失败')

    def tearDown(self):
        text = '-----step6:恢复环境; expect:成功-----'
        logger.info(text)
        logger.info('-----主节点恢复-----')
        self.rbcu.restore_guc_param(self.pri_hostname, self.recovery_time_target)
        logger.info('-----备1节点恢复-----')
        self.rbcu.restore_guc_param(self.sta_hostname, self.real_time)
        self.rbcu.restore_guc_param(self.sta_hostname, self.recovery_time_target)
        logger.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        logger.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败')
        logger.info(f'-----{os.path.basename(__file__)} end-----')
